package com.qian.controller;

import com.qian.pojo.Employees;
import com.qian.service.EmployeeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tenant")
public class AdminController {

    @Resource
    private EmployeeService employeeService;

    //获取所有租户
    @RequestMapping("/allTenants")
    public ResponseEntity allTenants(){
        HashMap res = new HashMap();
        res.put("allTenants",employeeService.allTenants());
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //添加租户
    @RequestMapping(value = "/addTenant",method = RequestMethod.POST)
    public ResponseEntity addTenant(@RequestBody Map map, HttpSession session){
        Employees employees = new Employees();
        int tenantId = Integer.parseInt( map.get("tenantId") + "");
        List<Integer> tenants = employeeService.allTenants();
        tenants.forEach(System.out::println);
        HashMap res = new HashMap();
        if(!tenants.contains(tenantId)){
            res.put("msg","无此租户,添加失败");
            return new ResponseEntity(res,HttpStatus.OK);
        }
        employees.setEmployeeAccount((String) map.get("employeeAccount"));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeCompany((String) map.get("employeeCompany"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employees.setTenantId(tenantId);
        employees.setAuthority(1);
        session.setAttribute("tenantId",tenantId);
        employeeService.addEmployee(employees);
        session.setAttribute("tenantId",0);
        res.put("msg","添加成功！");
        return new ResponseEntity(res,HttpStatus.OK);
    }

}
